package com.ruoyi.patrol.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtCheckAnswer;
import com.ruoyi.patrol.domain.PtCheckQuestion;
import com.ruoyi.patrol.mapper.PtCheckAnswerMapper;
import com.ruoyi.patrol.mapper.PtCheckQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.patrol.mapper.PtCheckRecordMapper;
import com.ruoyi.patrol.domain.PtCheckRecord;
import com.ruoyi.patrol.service.IPtCheckRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 专项检查Service业务层处理
 *
 * @author Du
 * @date 2023-11-18
 */
@Service
public class PtCheckRecordServiceImpl implements IPtCheckRecordService {
    @Autowired
    private PtCheckRecordMapper ptCheckRecordMapper;

    @Autowired
    private PtCheckQuestionMapper ptCheckQuestionMapper;

    @Autowired
    private PtCheckAnswerMapper ptCheckAnswerMapper;

    /**
     * 查询专项检查
     *
     * @param id 专项检查ID
     * @return 专项检查
     */
    @Override

    public PtCheckRecord selectPtCheckRecordById(Long id) {
        return ptCheckRecordMapper.selectPtCheckRecordById(id);
    }

    /**
     * 查询专项检查列表
     *
     * @param ptCheckRecord 专项检查
     * @return 专项检查
     */
    @Override
    public List<PtCheckRecord> selectPtCheckRecordList(PtCheckRecord ptCheckRecord) {
        return ptCheckRecordMapper.selectPtCheckRecordList(ptCheckRecord);
    }

    /**
     * 新增专项检查
     *
     * @param ptCheckRecord 专项检查
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPtCheckRecord(PtCheckRecord ptCheckRecord) {
        ptCheckRecord.setCreateTime(DateUtils.getNowDate());
        //查询问题集合
        PtCheckQuestion question = new PtCheckQuestion();
        question.setcId(ptCheckRecord.getCheckId());
        List<PtCheckQuestion> questionList = ptCheckQuestionMapper.selectPtCheckQuestionList(question);
        ptCheckRecordMapper.insertPtCheckRecord(ptCheckRecord);
        int i = 0;
        for (PtCheckQuestion questionItem : questionList) {
            PtCheckAnswer ptCheckAnswer = new PtCheckAnswer();
            ptCheckAnswer.setCheckRecordId(ptCheckRecord.getId());
            ptCheckAnswer.setQuestionName(questionItem.getQuestionName());
            ptCheckAnswerMapper.insertPtCheckAnswer(ptCheckAnswer);
            i++;
        }
        return questionList.size() == i ? 1 : -1;
    }

    /**
     * 修改专项检查
     *
     * @param ptCheckRecord 专项检查
     * @return 结果
     */
    @Override
    public int updatePtCheckRecord(PtCheckRecord ptCheckRecord) {
        ptCheckRecord.setUpdateTime(DateUtils.getNowDate());
        return ptCheckRecordMapper.updatePtCheckRecord(ptCheckRecord);
    }

    /**
     * 批量删除专项检查
     *
     * @param ids 需要删除的专项检查ID
     * @return 结果
     */
    @Override
    public int deletePtCheckRecordByIds(Long[] ids) {
        return ptCheckRecordMapper.deletePtCheckRecordByIds(ids);
    }

    /**
     * 删除专项检查信息
     *
     * @param id 专项检查ID
     * @return 结果
     */
    @Override
    public int deletePtCheckRecordById(Long id) {
        return ptCheckRecordMapper.deletePtCheckRecordById(id);
    }
}
